package com.eh.encription;

import com.sun.org.apache.xml.internal.security.utils.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
/**
 * 阐述对外接口安全方面的使用原则
 */
public class SecurityUtils {
    private static final String Algorithm = "DESede";
    private static final String PASSWORD_CRYPT_KEY = "2015MoankerVipshop";

    public SecurityUtils() {
    }

    public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException {
        byte[] key = new byte[24];
        byte[] temp = keyStr.getBytes("UTF-8");
        if(key.length > temp.length) {
            System.arraycopy(temp, 0, key, 0, temp.length);
        } else {
            System.arraycopy(temp, 0, key, 0, key.length);
        }

        return key;
    }

    public static byte[] decryptMode(byte[] src) {
        try {
            SecretKeySpec e = new SecretKeySpec(build3DesKey("2015MoankerVipshop"), "DESede");
            Cipher c1 = Cipher.getInstance("DESede");
            c1.init(2, e);
            return c1.doFinal(src);
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static String decryptMode(String str) {
        if(null != str && !"".equals(str.trim())) {
            try {
                return new String(decryptMode(Base64.decode(str)));
            } catch (Exception var2) {
                var2.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static String decryptMode(String str, String key) {
        if(null != str && !"".equals(str.trim())) {
            try {
                SecretKeySpec e = new SecretKeySpec(build3DesKey(key), "DESede");
                Cipher c1 = Cipher.getInstance("DESede");
                c1.init(2, e);
                return new String(c1.doFinal(Base64.decode(str)));
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static byte[] encryptMode(byte[] src) {
        try {
            SecretKeySpec e = new SecretKeySpec(build3DesKey("2015MoankerVipshop"), "DESede");
            Cipher c1 = Cipher.getInstance("DESede");
            c1.init(1, e);
            return c1.doFinal(src);
        } catch (Exception var3) {
            return null;
        }
    }

    public static String encryptMode(String str) {
        if(null != str && !"".equals(str.trim())) {
            byte[] secretArr = encryptMode(str.getBytes());
            return Base64.encode(secretArr);
        } else {
            return null;
        }
    }

    public static String encryptMode(String str, String key) {
        if(null != str && !"".equals(str.trim())) {
            try {
                SecretKeySpec e = new SecretKeySpec(build3DesKey(key), "DESede");
                Cipher c1 = Cipher.getInstance("DESede");
                c1.init(1, e);
                byte[] secretArr = c1.doFinal(str.getBytes());
                return Base64.encode(secretArr);
            } catch (Exception var5) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        String msg = "3DES加密解密案例";
        System.out.println("【加密前】：3DES加密解密案例");
        byte[] secretArr = encryptMode("3DES加密解密案例".getBytes());
        System.out.println("【加密后】：" + new String(secretArr));
        byte[] myMsgArr = decryptMode(secretArr);
        System.out.println("【解密后】：" + new String(myMsgArr));
        String secretStr = encryptMode("3DES加密解密案例");
        System.out.println("【加密后】：" + secretStr);
        System.out.println("【解密后】：" + decryptMode(secretStr));
    }
}

